001 /*
002 * Copyright 2005 Stephen J. McConnell.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.station.info;
020
021 import java.io.Serializable;
022 import java.net.URI;
023 import java.util.Arrays;
024
025 /**
026 * The RegistryDescriptor is immutable datastructure used to
027 * hold the state of an application registry.
028 *
029 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
030 * @version 1.0.1
031 */
032 public class RegistryDescriptor extends AbstractDescriptor
033 {
034 /**
035 * The default storage path.
036 */
037 public static final String DEFAULT_STORAGE_PATH = "local:xml:dpml/station/registry";
038
039 /**
040 * The default storage uri.
041 */
042 public static final URI DEFAULT_STORAGE_URI = createDefaultStorageURI();
043
044 private final Entry[] m_entries;
045
046 /**
047 * Creation of a new registry descriptor.
048 * @param entries an array of application entries
049 */
050 public RegistryDescriptor( Entry[] entries )
051 {
052 if( null == entries )
053 {
054 throw new NullPointerException( "entries" );
055 }
056 for( int i=0; i<entries.length; i++ )
057 {
058 if( null == entries[i] )
059 {
060 throw new NullPointerException( "entry" );
061 }
062 }
063
064 m_entries = entries;
065 }
066
067 /**
068 * Returns the array of application descriptors.
069 *
070 * @return the application entry array
071 */
072 public Entry[] getEntries()
073 {
074 return m_entries;
075 }
076
077 /**
078 * Compare this object with another for equality.
079 * @param other the object to compare this object with
080 * @return TRUE if the supplied object equivalent
081 */
082 public boolean equals( Object other )
083 {
084 if( !super.equals( other ) )
085 {
086 return false;
087 }
088 else if( !( other instanceof RegistryDescriptor ) )
089 {
090 return false;
091 }
092 else
093 {
094 RegistryDescriptor descriptor = (RegistryDescriptor) other;
095 return Arrays.equals( m_entries, descriptor.m_entries );
096 }
097 }
098
099 /**
100 * Return the hashcode for the object.
101 * @return the hashcode value
102 */
103 public int hashCode()
104 {
105 int hash = super.hashCode();
106 hash ^= hashArray( m_entries );
107 return hash;
108 }
109
110 /**
111 * Return a string representation of the registry.
112 * @return the string value
113 */
114 public String toString()
115 {
116 StringBuffer buffer = new StringBuffer( "[registry " );
117 for( int i=0; i<m_entries.length; i++ )
118 {
119 if( i != 0 )
120 {
121 buffer.append( ", " );
122 }
123 Entry entry = m_entries[i];
124 buffer.append( entry.toString() );
125 }
126 return buffer.toString();
127 }
128
129 private static URI createDefaultStorageURI()
130 {
131 try
132 {
133 return new URI( DEFAULT_STORAGE_PATH );
134 }
135 catch( Exception e )
136 {
137 return null; // will not happen
138 }
139 }
140
141 /**
142 * Binding of key to descriptor.
143 */
144 public static final class Entry implements Serializable
145 {
146 private final String m_key;
147 private final ApplicationDescriptor m_descriptor;
148
149 /**
150 * Creation of a new entry.
151 * @param key the profile key
152 * @param descriptor the application descriptor
153 */
154 public Entry( String key, ApplicationDescriptor descriptor )
155 {
156 if( null == key )
157 {
158 throw new NullPointerException( "key" );
159 }
160 if( null == descriptor )
161 {
162 throw new NullPointerException( "descriptor" );
163 }
164 m_key = key;
165 m_descriptor = descriptor;
166 }
167
168 /**
169 * Return the entry key.
170 * @return the key
171 */
172 public String getKey()
173 {
174 return m_key;
175 }
176
177 /**
178 * Return the application descriptor.
179 * @return the application descriptor
180 */
181 public ApplicationDescriptor getApplicationDescriptor()
182 {
183 return m_descriptor;
184 }
185
186 /**
187 * Tests for equality. Two entries are considered equal if
188 * they have the same key and descriptor.
189 *
190 * @param other the other object
191 * @return the equality status
192 */
193 public boolean equals( Object other )
194 {
195 if( null == other )
196 {
197 return false;
198 }
199 else if( other instanceof Entry )
200 {
201 Entry entry = (Entry) other;
202 if( !m_key.equals( entry.getKey() ) )
203 {
204 return false;
205 }
206 else
207 {
208 return m_descriptor.equals( entry.m_descriptor );
209 }
210 }
211 else
212 {
213 return false;
214 }
215 }
216
217 /**
218 * Compute the hashcode.
219 * @return the hashcode value
220 */
221 public int hashCode()
222 {
223 int hash = m_key.hashCode();
224 hash ^= m_descriptor.hashCode();
225 return hash;
226 }
227
228 /**
229 * Return a string representation of the registry.
230 * @return the string value
231 */
232 public String toString()
233 {
234 return "[entry key="
235 + m_key
236 + " descriptor="
237 + m_descriptor.toString()
238 + " ]";
239 }
240 }
241 }